package com.gzmu.face.serviceImpl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.gzmu.face.entity.Device;
import com.gzmu.face.entity.Faceinfo;
import com.gzmu.face.mapper.FaceinfoMapper;
import com.gzmu.face.service.DeviceService;
import com.gzmu.face.service.FaceinfoService;
import com.gzmu.face.tools.HttpResult;
import com.gzmu.face.tools.HttpUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xiaobo Yan
 * @since 2020-05-07
 */
@Service
public class FaceinfoServiceImpl extends ServiceImpl<FaceinfoMapper, Faceinfo> implements FaceinfoService {

    @Autowired
    private DeviceService deviceService;
    @Autowired
    private FaceinfoMapper faceinfoMapper;

    @Transactional
    @Override
    public Map insertFaceinfoUp(Faceinfo faceinfo) {
        JSONArray roomStr = (JSONArray) JSON.parse(faceinfo.getRoom());
        Map<String, Object> map = new HashMap<>();
        map.put("code",201);
        for (int i = 0; i<roomStr.size();i++ ) {
            JSONObject room = (JSONObject) roomStr.get( i );
            int id = room.getIntValue( "value" );
            Device device = deviceService.selectById( id );
            if (device != null) {
                faceinfo.setFaceid( device.getDeviceID() );
                faceinfo.setRemark( 0 );
                faceinfoMapper.insertFaceInfo( faceinfo );
                Map<String, String> headParams = new HashMap<>();
                headParams.put( "name", "admin" );
                headParams.put( "passwd", "admin" );

                ObjectMapper mapper = new ObjectMapper();
                try {
                    System.out.println( "headParams:" + mapper.writeValueAsString( headParams ) );
                    HttpResult result = HttpUtil.doPost( "http://" + device.getIPAddr() + "/action/AddPerson",
                            headParams, mapper.writeValueAsString( headParams ) );
                    if (result.getStatusCode() == 200) {
                        JSONObject jsonObject = JSONObject.parseObject( result.getBody() );
                        map.put( "code", jsonObject.get( "code" ) );
                        map.put( "info", jsonObject.get( "info" ) );
                    }
                } catch (Exception e) {
                    map.put( "code", 202 );
                    e.printStackTrace();
                }
            }
        }
        return map;
    }

    @Transactional
    @Override
    public Map deleteFaceinfoUp(String id) {
        Map<String, Object> map = new HashMap<>();
        map.put("code",201);
        Faceinfo faceinfo = faceinfoMapper.selectById( id );
        Device device = deviceService.selectById(faceinfo.getRoom());
        Map<String,String> headParams = new HashMap<>();
        headParams.put( "name","admin" );
        headParams.put( "passwd", "admin" );
        return map;
    }
}
